package com.meida.module.bus.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.module.bus.client.entity.VipConfig;
import com.meida.module.bus.client.entity.VipType;
import com.meida.module.bus.provider.mapper.VipTypeMapper;
import com.meida.module.bus.provider.service.VipConfigService;
import com.meida.module.bus.provider.service.VipTypeService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * vip表接口实现类
 *
 * @author flyme
 * @date 2022-06-30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VipTypeServiceImpl extends BaseServiceImpl<VipTypeMapper, VipType> implements VipTypeService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, VipType type, EntityMap extra) {
        return ResultBody.ok();
    }

    @Autowired
    VipConfigService vipConfigService;


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<VipType> cq, VipType type, EntityMap params) {
        cq.select(VipConfig.class, "configTitle", "configContent");
        cq.select(VipType.class, "*");
        cq.createJoin(VipConfig.class).setMainField("vipTypeId").setJoinField("vipTypeId");
        cq.orderByDesc("type.createTime");
        cq.like(ObjectUtil.isNotNull(params.get("vipName")), "type.vipName", params.get("vipName"));
        cq.ge(ObjectUtil.isNotEmpty(params.get("beginDate")), "type.createTime", params.get("beginDate"));
        cq.le(ObjectUtil.isNotEmpty(params.get("endDate")), "type.createTime", params.get("endDate"));

        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public ResultBody afterAdd(CriteriaSave cs, VipType save, EntityMap extra) {
        Long vipTypeId = save.getVipTypeId();
        String title = cs.getParams("configTitle");
        String content = cs.getParams("configContent");

        VipConfig details = new VipConfig();
        details.setVipTypeId(vipTypeId);
        details.setConfigTitle(title);
        details.setConfigContent(content);
        vipConfigService.save(details);

        return ResultBody.ok("添加成功");
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public ResultBody afterEdit(CriteriaUpdate cs, VipType up, EntityMap extra) {
        vipConfigService.update( new UpdateWrapper<VipConfig>().set("configTitle",cs.getParams("configTitle")).set("configContent",cs.getParams("configContent")).eq("vipTypeId",up.getVipTypeId()));
        return ResultBody.ok("编辑成功");
    }


    @SneakyThrows
    @Override
    public void afterGet(CriteriaQuery cq, EntityMap result) {
         VipConfig vipConfig = vipConfigService.getOne(new QueryWrapper<VipConfig>().lambda().eq(VipConfig::getVipTypeId, result.getLong("vipTypeId")));

        if (FlymeUtils.isNotEmpty(vipConfig)) {
            //商品详情
             result.put ("configTitle",vipConfig.getConfigTitle());
             result.put ("configContent",vipConfig.getConfigContent());
        }

    }
}
